package com.liu.frame.application;

import android.app.Application;

import com.google.gson.Gson;
import com.liu.frame.MyEventBusIndex;
import com.liu.frame.beans.UserBean;
import com.liu.frame.cache.CacheConfig;
import com.liu.frame.config.Api;
import com.liu.frame.config.Constant;
import com.liu.frame.log.LogCrash;
import com.liu.frame.utils.AppUtils;
import com.liu.frame.utils.SharedPreferencesUtils;
import com.liu.frame.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import cn.finalteam.okhttpfinal.OkHttpFinal;
import cn.finalteam.okhttpfinal.OkHttpFinalConfiguration;

/**
 * Created by 刘智强 on 2016/10/19.
 */
public class MyApplication extends Application {
    public static MyApplication instance;
    /**
     * 登录信息实体
     */
    private UserBean mUser = null;

    /**
     * app版本号
     */
    public String appVersion="";

    private String imei;//imei
    /**
     * 注册信息实体
     */
    private double localVersion;// 本地安装版本
    private double serverVersion;// 服务器版本
    /**
     * 单例模式构造Myapplication对象
     */
    public static MyApplication getInstance(){
        if (instance==null){
            instance=new MyApplication();
        }
        return  instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance=this;
//        mUser=new Gson().fromJson(CacheConfig.getInstance().getJsonString(Api.LOGIN),UserBean.class);
        mUser=new SharedPreferencesUtils(instance).getObject(Api.LOGIN,UserBean.class);
        if (mUser!=null){
            setmUser(mUser);
        }else {
            ToastUtils.myToast(instance,"您还没登陆，请先登陆!!!");
        }
        appVersion = AppUtils.getVersionCode(instance);
//        initLog();
//        initJiGuangPush();
//        initYouMeng();
        initOkhttpFinal();
//        Fresco.initialize(this);//初始化Fresco
        // 启用EventBus3.0加速功能
        EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();


    }


    /**
     * init crash log
     */
    private void initLog() {
        LogCrash logCrash = LogCrash.getInstance();
        logCrash.init(this, Constant.FILE_LOG_ERROR);

    }

//    /**
//     * 初始化极光推送
//     */
//    public  void initJiGuangPush(){
//        //初始化sdk
//        JPushInterface.setDebugMode(true);//正式版的时候设置false，关闭调试
//        JPushInterface.init(this);
//        //建议添加tag标签，发送消息的之后就可以指定tag标签来发送了
//        Set<String> set = new HashSet<>();
//        set.add("andfixdemo");//名字任意，可多添加几个
//        JPushInterface.setTags(this, set, null);//设置标签
//    }
//
//    /**
//     * 友盟统计的相关设置
//     */
//    public void initYouMeng(){
//        MobclickAgent.setDebugMode(false);//友盟启动，是否输出日志
//        MobclickAgent.enableEncrypt(true);//6.0.0版本及以后,对日志进行加密
//    }

    private   void initOkhttpFinal(){
        OkHttpFinalConfiguration.Builder builder = new OkHttpFinalConfiguration.Builder();
        OkHttpFinal.getInstance().init(builder.build());
    }

    public  boolean isLogin(){
        if (new SharedPreferencesUtils(this).getObject(Api.LOGIN,UserBean.class)!=null){
            return  true;
        }

        return  false;

    }

    public UserBean getmUser() {
        return mUser;
    }

    public void setmUser(UserBean mUser) {
        this.mUser = mUser;
    }

    public double getLocalVersion() {
        return localVersion;
    }

    public void setLocalVersion(double localVersion) {
        this.localVersion = localVersion;
    }

    public double getServerVersion() {
        return serverVersion;
    }

    public void setServerVersion(double serverVersion) {
        this.serverVersion = serverVersion;
    }
}
